﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using deFramework;

namespace deFramework.Base
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseBusinessRule 
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aContext"></param>
        public BaseBusinessRule(ref Common.PresentationContext aContext)
        {
            try
            {
                this.AssignContext(ref aContext);
                this.AssignSpWeb(ref aContext);
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            //this.MyContext = aContext;
            //this.MySpWeb = aContext.MySpWeb; 
        }


        protected string ElevatedSharePointProcessing(MethodNoParameters methodProcessing)
        {
            string _result = String.Empty;
            Microsoft.SharePoint.SPWeb webCurrent = this.MyContext.MySpWeb;
            try
            {
                Microsoft.SharePoint.SPSite siteColl = this.MyContext.MySpWeb.Site;
                Microsoft.SharePoint.SPWeb siteWeb = this.MyContext.MySpWeb;
                Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (Microsoft.SharePoint.SPSite elevatedsiteColl = new Microsoft.SharePoint.SPSite(siteColl.ID))
                    {
                        using (Microsoft.SharePoint.SPWeb elevatedSite = elevatedsiteColl.OpenWeb(siteWeb.ID))
                        {
                            try
                            {
                                this.MyContext.MySpWeb = elevatedSite; 
                                _result = methodProcessing();
                            }
                            catch (Exception ex)
                            {
                                this.CurrentException = ex;
                            }
                        }
                    }
                });

            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            finally
            {
                Microsoft.SharePoint.SPWeb tempWeb = this.MyContext.MySpWeb;
                tempWeb.Dispose();
                this.MyContext.MySpWeb = webCurrent;
            }
            return _result;
        }



        public delegate string MethodNoParameters();

        private void AssignContext(ref Common.PresentationContext aContext)
        {
            this.MyContext = aContext;
        }

        private void AssignSpWeb(ref Common.PresentationContext aContext)
        {
            this.MySpWeb = aContext.MySpWeb;
        }

        /// <summary>
        /// 
        /// </summary>
        protected Common.PresentationContext MyContext = null;
        
        /// <summary>
        /// 
        /// </summary>
        protected Microsoft.SharePoint.SPWeb MySpWeb = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        protected Microsoft.SharePoint.SPWeb GetSpWeb(string url)
        {
            Microsoft.SharePoint.SPSite site = new Microsoft.SharePoint.SPSite(url);
            Microsoft.SharePoint.SPWeb web = site.OpenWeb();
            return web;
        }



        //public Microsoft.SharePoint.SPWeb OpenSiteAsSpWeb(string url)
        //{
        //    Microsoft.SharePoint.SPSite objSite = new Microsoft.SharePoint.SPSite(url);
        //    string webUrl = url.Substring(objSite.Url.Length);
        //    return objSite.OpenWeb(webUrl); //this.textBoxUrl.Text); 
        //}


        #region "Polymorphism - PageParamValue, SpWebPropertyValue, SetSpWebProperty, RemoveSpWebProperty"

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aVariableName"></param>
        /// <param name="aDefault"></param>
        /// <returns></returns>
        public string PageParamValue(string aVariableName, string aDefault)
        {
            return this.MyContext.PageParamValue(aVariableName, aDefault);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aPropertyName"></param>
        /// <param name="aDefault"></param>
        /// <returns></returns>
        public string SpWebPropertyValue(string aPropertyName, string aDefault)
        {
            return this.MyContext.SpWebPropertyValue(aPropertyName, aDefault);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aPropertyName"></param>
        /// <param name="aValue"></param>
        /// <returns></returns>
        public bool SetSpWebProperty(string aPropertyName, string aValue)
        {
            return this.MyContext.SetSpWebProperty(aPropertyName, aValue);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aPropertyName"></param>
        /// <returns></returns>
        public bool RemoveSpWebProperty(string aPropertyName)
        {
            return this.MyContext.RemoveSpWebProperty(aPropertyName);
        }


        #endregion

        #region "Polymorphism - MyLog, CurrentException, StandardLog, GotoErrorPage"

        /// <summary>
        /// 
        /// </summary>
        public Common.FrameworkLogging MyLog
        {
            get { return this.MyContext.MyLog; }
        }

        /// <summary>
        /// 
        /// </summary>
        public System.Exception CurrentException
        {
            get
            {
                Exception _ex = null;
                try
                {
                    _ex = this.MyContext.CurrentException;
                }
                catch (Exception ex)
                {
                    _ex = ex;
                }
                return _ex;
            }
            set
            {
                Exception _ex = value;
                try
                {
                    this.MyContext.CurrentException = _ex;
                }
                catch
                {
                    // do nothing 
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aMethodName"></param>
        /// <param name="aMessage"></param>
        /// <returns></returns>
        public bool StandardLog(string aMethodName, string aMessage)
        {
            return this.MyContext.StandardLog(aMethodName, aMessage);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aTitle"></param>
        /// <param name="aDescription"></param>
        public void GotoErrorPage(string aTitle, string aDescription)
        {
            this.MyContext.GotoErrorPage(aTitle, aDescription);
        }



        #endregion 
     
        #region "Methods to go into Base Biz Rule library"

        /// <summary>
        /// 
        /// </summary>
        /// <param name="infoPathFormUrl"></param>
        /// <param name="itemFile"></param>
        /// <returns></returns>
        protected System.Xml.XmlDocument LoadSpFileItemAsInfoPathXmlDoc(string infoPathFormUrl, out Microsoft.SharePoint.SPListItem itemFile)
        {
            itemFile = null;
            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
            try
            {
                string url = infoPathFormUrl.Trim();
                if (url.IndexOf(",") > 1)
                    url = url.Substring(0, url.IndexOf(",") - 1).Trim();
                Microsoft.SharePoint.SPFile file = this.GetSpFileForUrl(url);
                itemFile = file.Item;
                xd.PreserveWhitespace = true;
                if (!(file == null))
                {
                    using (System.IO.MemoryStream inStream =
                        new System.IO.MemoryStream(file.OpenBinary()))
                    {
                        xd.Load(inStream);
                    }
                }
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return xd;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        protected Microsoft.SharePoint.SPFile GetSpFileForUrl(string url)
        {
            Microsoft.SharePoint.SPFile fileAtUrl = this.MyContext.MySpWeb.GetFile(url);
            if (fileAtUrl.Exists)
                return fileAtUrl;
            else
                throw new Exception("Unable to find Form file at " + url);
        }


        #endregion 
 
    }
}
